<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<!--准备好一个容器-->
<div id="test">

</div>
<!--核心库引入顺序，必须先是核心库然后在引入dom库-->
<script src="../js/react.development.js" crossorigin></script>
<!--用于支持react操作dom-->
<script src="../js/react-dom.development.js"></script>
<!--引入babel，用于将jsx转为js-->
<script src="../js/babel.min.js"></script>
<script type="text/babel">
    class Person extends React.Component {
        state = {
            persons: [
                {id: 1, name: '小周', age: 18},
                {id: 2, name: '小李', age: 22}
            ]
        }

        add = () => {
            let {persons} = this.state
            let p = {id: persons.length + 1, name: '小王', age: 10}
            this.setState({
                persons: [p, ...persons]
            })
        }

        render() {
            return (
                <div>
                    <h2>展示人员信息</h2>
                    <button onClick={this.add}>添加一个小王</button>
                    <ul>
                        {
                            this.state.persons.map((value, index) => {
                                return <li key={index}>{value.name},{value.age}</li>
                            })
                        }
                    </ul>
                </div>
            );
        }
    }

    ReactDOM.render(<Person/>, document.getElementById('test'))
</script>
</body>
</html>